package com.meadin.agent.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;

import com.meadin.agent.domain.HttpEntitiy;

public class HttpUtils {
	// 用代理发送get请求
	public static String sendGet(String url) {
		BufferedReader in = null;
		String result = "";
		try {
			if (StringUtils.isBlank(url)) {
				return "";

			}
			HttpEntitiy httpEntitiy = GetProxy.getProxy(url);
			HttpGet get = httpEntitiy.getGet();
			CloseableHttpClient client = httpEntitiy.getClient();
			get.setHeader("Accept", "text/javascript, text/html, application/xml, text/xml, */*");
			get.setHeader("connection", "Keep-Alive");
			get.setHeader("Accept-Encoding", "gzip, deflate, sdch, br");
			get.setHeader("Accept-Language", "zh-CN,zh;q=0.8");
			get.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");

			HttpResponse httpResponse = client.execute(get);

			InputStream ins = httpResponse.getEntity().getContent();
			InputStreamReader isr = new InputStreamReader(ins, "utf-8");
			in = new BufferedReader(isr);

			String line;
			while ((line = in.readLine()) != null) {
				result += line;
			}
			return result;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;

	}
}
